﻿using EWSEditor.Exchange;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Threading;

namespace EWSEditor.CRM.Forms
{
    public partial class MailboxDiagnostic : Form
    {
        public MailboxDiagnostic()
        {
            InitializeComponent();
            ListImages = new ImageList();
            InitializeImageList();
            SetListImages();
            Thread TreeBuilder = new Thread(new ThreadStart(BuildTree));
            TreeBuilder.SetApartmentState(ApartmentState.STA);
            TreeBuilder.Start();
        }

        public ImageList ListImages
        {
            get { return _listImages; }
            set { _listImages = value; }
        }

        private ImageList _listImages;

        public bool OrgError = false;
        public bool OrgWarning = false;
        public bool MailboxError = false;
        public bool MailboxWarning = false;
        public bool ACTWarning = false;
        public bool ACTError = false;
        public bool MiscError = false;
        public bool MiscWarning = false;
        public bool MailboxUsingSSS = false;
        public bool MailboxUsingACT = false;
        public bool MailboxUsingOutlook = false;
        public bool MailboxUsingRouter = false;
        public bool MailboxApproved = false;

        private void InitializeImageList()
        {
            ListImages.Images.Add(Properties.Resources.GreenCheck);
            ListImages.Images.Add(Properties.Resources.warning);
            ListImages.Images.Add(Properties.Resources.error);
            ListImages.Images.Add(Properties.Resources.information);
        }

        private void MailboxDiagnostic_Load(object sender, EventArgs e)
        {
        }

        private void BuildTree()
        {
            //this.Invoke(new Action<string>(SetProgressLabel), "Refreshing Mailbox Data...");
            RefreshData();
            SetTreeProperties();
            this.Invoke(new Action(BuildOrganization));
            //this.Invoke(new Action<int>(IncrementProgress), 40);
            //this.Invoke(new Action<string>(SetProgressLabel), "Checking Mailbox...");
            this.Invoke(new Action(BuildMailboxSettingsNode));
            //this.Invoke(new Action<int>(IncrementProgress), 75);
            //this.Invoke(new Action<string>(SetProgressLabel), "Checking Misc Settings...");
            this.Invoke(new Action(BuildMisc));
            this.Invoke(new Action(SetHeaders));
            //this.Invoke(new Action<string>(SetProgressLabel), "Done");
            this.Invoke(new Action<int>(IncrementProgress), 100);
        }

        private void RefreshData()
        {
            CrmOrgMailboxCache.RefreshCache();
        }

        private void IncrementProgress(int percentage)
        {
            //prgLoad.Value = percentage;

            if (percentage == 100)
            {
                pnlLoading.Visible = false;
                lblLoading.Visible = false;
            }
        }

        private void SetProgressLabel(string text)
        {
            //lblLoad.Text = text;
        }

        private void SetListImages()
        {
            diagnosticTree.ImageList = ListImages;
        }

        private void SetTreeProperties()
        {
            if (((OptionSetValue)CrmOrgMailboxCache.TargetMailbox["actdeliverymethod"]).Value == 1)
            {
                MailboxUsingACT = true;
            }

            if (((OptionSetValue)CrmOrgMailboxCache.Organization["emailconnectionchannel"]).Value == 0 && ((OptionSetValue)CrmOrgMailboxCache.TargetMailbox["incomingemaildeliverymethod"]).Value == 2
               || ((OptionSetValue)CrmOrgMailboxCache.TargetMailbox["incomingemaildeliverymethod"]).Value == 3)
            {
                MailboxUsingSSS = true;
            }

            if (((OptionSetValue)CrmOrgMailboxCache.TargetMailbox["incomingemaildeliverymethod"]).Value == 1)
            {
                MailboxUsingOutlook = true;
            }
            if (((OptionSetValue)CrmOrgMailboxCache.Organization["emailconnectionchannel"]).Value == 1 && ((OptionSetValue)CrmOrgMailboxCache.TargetMailbox["incomingemaildeliverymethod"]).Value == 2
                || ((OptionSetValue)CrmOrgMailboxCache.TargetMailbox["incomingemaildeliverymethod"]).Value == 3)
            {
                MailboxUsingRouter = true;
            }
            if (((OptionSetValue)CrmOrgMailboxCache.TargetMailbox["emailrouteraccessapproval"]).Value == 1)
            {
                MailboxApproved = true;
            }
        }

        private void BuildOrganization()
        {
            TreeNode header = new TreeNode("Organization Settings");
            diagnosticTree.Nodes.Add(header);
            diagnosticTree.Nodes[0].Nodes.Add("Ignore Internal Email");
            DetermineInternalEmail();
            diagnosticTree.Nodes[0].Nodes.Add("Connection Channel");
            DetermineConnectionChannel();
            diagnosticTree.Nodes[0].Nodes.Add("E-mail Correlation Enabled");
            DetermineCorrelationEnabled();
            diagnosticTree.Nodes[0].Nodes.Add("OrgDBOrgSettings");
            DetermineOrgDBOrgSettings();
            diagnosticTree.Nodes[0].Nodes.Add("Tracking Token & Smart Matching Settings");
            DetermineCorrelationMethods();
        }

        private void DetermineInternalEmail()
        {
            if ((bool)CrmOrgMailboxCache.Organization["ignoreinternalemail"])
            {
                diagnosticTree.Nodes[0].Nodes[0].ImageIndex = 1;
                diagnosticTree.Nodes[0].Nodes[0].SelectedImageIndex = 1;
                diagnosticTree.Nodes[0].Nodes[0].ToolTipText = "E-mails will not track automatically between CRM users. This setting can be changed in the System Settings.\n\nOrganization.IgnoreInternalEmail = true";
                OrgWarning = true;
                //ToolTip = true;
            }

        }
        private void DetermineConnectionChannel()
        {
            diagnosticTree.Nodes[0].Nodes[1].ImageIndex = 3;
            diagnosticTree.Nodes[0].Nodes[1].SelectedImageIndex = 3;
            string connectionchanneltext = MetadataHelpers.GetOptionSetValueLabel("organization", "emailconnectionchannel", ((OptionSetValue)CrmOrgMailboxCache.Organization["emailconnectionchannel"]).Value);
            diagnosticTree.Nodes[0].Nodes[1].ToolTipText = connectionchanneltext;
            return;
        }

        private void DetermineCorrelationMethods()
        {
            if ((int)CrmOrgMailboxCache.Organization["maximumtrackingnumber"] == 0 && !CrmOrgMailboxCache.Organization.Contains("enablesmartmatching"))
            {
                diagnosticTree.Nodes[0].Nodes[4].ImageIndex = 1;
                diagnosticTree.Nodes[0].Nodes[4].SelectedImageIndex = 1;
                diagnosticTree.Nodes[0].Nodes[4].ToolTipText = "Both tracking token and Smart Matching are disabled. This will inhibit CRM from ensuring that tracked email responses are correctly tracked for users or queues that are set to promote e-mail in response to CRM e-mail.\n\nOrganization.MaximumTrackingNumber = 0\nOrganization.EnableSmartMatching = false";
                OrgWarning = true;
            }
        }

        private void DetermineCorrelationEnabled()
        {
            if (!(bool)CrmOrgMailboxCache.Organization["emailcorrelationenabled"])
            {
                diagnosticTree.Nodes[0].Nodes[2].ImageIndex = 1;
                diagnosticTree.Nodes[0].Nodes[2].SelectedImageIndex = 1;
                diagnosticTree.Nodes[0].Nodes[2].ToolTipText = "E-mail Correlation allows Server Side Sync to utilize In-Reply-To and ConversationIndex message headers to provide greater flexibility when correlating e-mail messages in response to CRM e-mail. Disabling this setting is not recommended.\n\nOrganization.EmailCorrelationEnabled = false";
                OrgWarning = true;
            }
        }

        private void DetermineOrgDBOrgSettings()
        {
            //To-Do: Parse OrgDBOrgSettings and display as child nodes.
            diagnosticTree.Nodes[0].Nodes[3].ImageIndex = 3;
            diagnosticTree.Nodes[0].Nodes[3].SelectedImageIndex = 3;
            if (CrmOrgMailboxCache.Organization.Contains("orgdborgsettings"))
            {
                diagnosticTree.Nodes[0].Nodes[3].ToolTipText = (string)CrmOrgMailboxCache.Organization["orgdborgsettings"];
            }
            else
            {
                diagnosticTree.Nodes[0].Nodes[3].ToolTipText = "No OrgDBOrgSettings have been implemented";
            }
        }

        private void BuildMailboxSettingsNode()
        {
            TreeNode header = new TreeNode("Mailbox Settings");
            diagnosticTree.Nodes.Add(header);

            if (MailboxUsingSSS)
            {
                diagnosticTree.Nodes[1].Nodes.Add("Server Side Synchronization Incoming E-mail Diagnostics");
                diagnosticTree.Nodes[1].Nodes[0].Nodes.Add("Incoming Email Status");
                DetermineIncomingEmailStatus();
                diagnosticTree.Nodes[1].Nodes[0].Nodes.Add("Enabled For Incoming");
                DetermineEnabledForIncoming();
                diagnosticTree.Nodes[1].Nodes[0].Nodes.Add("Mailbox Processing Stalled");
                DetermineStuckMailbox();
                diagnosticTree.Nodes[1].Nodes[0].Nodes.Add("Latency in Mailbox Processing");
                DetermineReceivingPostPonedUntil();
                diagnosticTree.Nodes[1].Nodes[0].Nodes.Add("Incoming Delivery Method");
                DetermineIncomingDeliveryMethod();
                diagnosticTree.Nodes[1].Nodes[0].Nodes.Add("Email Address Approval");
                DetermineEmailApproval();

            }
            else if (MailboxUsingRouter)
            {
                diagnosticTree.Nodes[1].Nodes.Add("E-mail Router Mailbox Diagnostics");
                diagnosticTree.Nodes[1].Nodes[0].Nodes.Add("Incoming Delivery Method");
                DetermineIncomingDeliveryMethod();
                //diagnosticTree.Nodes[1].Nodes[0].Nodes.Add("Email Address Match");
                //DetermineEmailAddress();
                diagnosticTree.Nodes[1].Nodes[0].Nodes.Add("Email Address Approval");
                DetermineEmailApproval();
            }

            else
            {
                diagnosticTree.Nodes[1].Nodes.Add("General Mailbox Diagnostics");
                diagnosticTree.Nodes[1].Nodes[0].Nodes.Add("Incoming Delivery Method");
                DetermineIncomingDeliveryMethod();
                //diagnosticTree.Nodes[1].Nodes[0].Nodes.Add("Email Address Match");
                //DetermineEmailAddress();
                diagnosticTree.Nodes[1].Nodes[0].Nodes.Add("Email Address Approval");
                DetermineEmailApproval();
            }

            if (MailboxUsingACT)
            {
                diagnosticTree.Nodes[1].Nodes.Add("ACT Synchronization Diagnostics");
                diagnosticTree.Nodes[1].Nodes[1].Nodes.Add("ACT Status");
                DetermineACTStatus();
            }
        }

        private void DetermineACTStatus()
        {
            if (((OptionSetValue)CrmOrgMailboxCache.TargetMailbox["actstatus"]).Value == 0)
            {
                diagnosticTree.Nodes[1].Nodes[1].Nodes[0].ImageIndex = 1;
                diagnosticTree.Nodes[1].Nodes[1].Nodes[0].SelectedImageIndex = 1;
                diagnosticTree.Nodes[1].Nodes[1].Nodes[0].ToolTipText = "ACT Processing has not been enabled.\n\nmailbox.actstatus = 0";
                MailboxWarning = true;
                ACTWarning = true;
                return;
            }

            if (((OptionSetValue)CrmOrgMailboxCache.TargetMailbox["actstatus"]).Value == 2)
            {
                diagnosticTree.Nodes[1].Nodes[1].Nodes[0].ImageIndex = 2;
                diagnosticTree.Nodes[1].Nodes[1].Nodes[0].SelectedImageIndex = 2;
                diagnosticTree.Nodes[1].Nodes[1].Nodes[0].ToolTipText = "ACT Test and Enable failed. Please check the mailbox alerts for error information. \n\nmailbox.actstatus = 2";
                MailboxError = true;
                ACTError = true;
                return;
            }
        }

        private void DetermineIncomingEmailStatus()
        {
            if (((((OptionSetValue)CrmOrgMailboxCache.TargetMailbox["incomingemaildeliverymethod"]).Value == 3) || ((OptionSetValue)CrmOrgMailboxCache.TargetMailbox["incomingemaildeliverymethod"]).Value == 2))
            {
                if (((OptionSetValue)CrmOrgMailboxCache.TargetMailbox["incomingemailstatus"]).Value == 0)
                {
                    diagnosticTree.Nodes[1].Nodes[0].Nodes[0].ToolTipText = "Incoming Email processing is disabled. You may need to Test & Enable the mailbox.\n\nMailbox.IncomingEmailStatus = 0 (Not Run)";
                    diagnosticTree.Nodes[1].Nodes[0].Nodes[0].ImageIndex = 1;
                    diagnosticTree.Nodes[1].Nodes[0].Nodes[0].SelectedImageIndex = 1;
                    MailboxWarning = true;
                    return;
                }
                else if (((OptionSetValue)CrmOrgMailboxCache.TargetMailbox["incomingemailstatus"]).Value == 2)
                {
                    diagnosticTree.Nodes[1].Nodes[0].Nodes[0].ToolTipText = "The mailbox failed Test Access. Please check the mailbox alerts and server traces for more information.\n\nMailbox.IncomingEmailStatus = 2 (Failed)";
                    diagnosticTree.Nodes[1].Nodes[0].Nodes[0].ImageIndex = 2;
                    diagnosticTree.Nodes[1].Nodes[0].Nodes[0].SelectedImageIndex = 2;
                    MailboxError = true;
                }
            }
        }
        private void DetermineEnabledForIncoming()
        {
            if (!(bool)CrmOrgMailboxCache.TargetMailbox["enabledforincomingemail"])
            {
                diagnosticTree.Nodes[1].Nodes[0].Nodes[1].ImageIndex = 1;
                diagnosticTree.Nodes[1].Nodes[0].Nodes[1].SelectedImageIndex = 1;
                diagnosticTree.Nodes[1].Nodes[0].Nodes[1].ToolTipText = "The mailbox is not currently enabled for Incoming Email Processing. A System Administrator will need to Approve and  Test & Enable the mailbox.\n\nMailbox.EnabledForIncomingEmail = false";
                MailboxWarning = true;
            }
        }
        private void DetermineStuckMailbox()
        {
            if (((((DateTime)CrmOrgMailboxCache.TargetMailbox["postponemailboxprocessinguntil"]).Date == DateTime.MaxValue.Date) && (int)CrmOrgMailboxCache.TargetMailbox["processingstatecode"] == 0))
            {
                diagnosticTree.Nodes[1].Nodes[0].Nodes[2].ImageIndex = 2;
                diagnosticTree.Nodes[1].Nodes[0].Nodes[2].SelectedImageIndex = 2;
                diagnosticTree.Nodes[1].Nodes[0].Nodes[2].ToolTipText = "The mailbox is not currently processing e-mail and is considered inactive. The mailbox may have encountered an error during processing or has been reset through a change on the mailbox record. Check the mailbox alerts and server traces for more information.\n\nMailbox.PostponeMailboxProcessingUntil = DateTime.Max\n\nMailbox.ProcessingStateCode = 0";
                MailboxError = true;
            }
        }
        private void DeterminePrimaryOrg()
        {
            if (!(bool)CrmOrgMailboxCache.TargetMailbox["orgmarkedasprimaryforexchangesync"])
            {
                diagnosticTree.Nodes[1].Nodes[0].Nodes[3].ImageIndex = 1;
                diagnosticTree.Nodes[1].Nodes[0].Nodes[3].SelectedImageIndex = 1;
                MailboxWarning = true;
                diagnosticTree.Nodes[1].Nodes[0].Nodes[3].ToolTipText = "The configured organization is not the primary organization for Server Side Synchronization for the target mailbox. To set this, Test & Enable the mailbox and ensure Marked As Primary is checked.";
            }
        }
        private void DetermineReceivingPostPonedUntil()
        {
            DateTime date = new DateTime();
            date = DateTime.UtcNow;
            if ((DateTime)CrmOrgMailboxCache.TargetMailbox["receivingpostponeduntil"] > date.Add(new TimeSpan(0, 12, 0, 0)))
            {
                diagnosticTree.Nodes[1].Nodes[0].Nodes[3].ImageIndex = 1;
                diagnosticTree.Nodes[1].Nodes[0].Nodes[3].SelectedImageIndex = 1;
                diagnosticTree.Nodes[1].Nodes[0].Nodes[3].ToolTipText = "Mailbox incoming processing is postponed beyond 12 hours from the current time.\n\nMailbox.ReceivingPostPonedUntil";
                MailboxWarning = true;
            }
        }

        private void DetermineIncomingDeliveryMethod()
        {
            if (((OptionSetValue)CrmOrgMailboxCache.TargetMailbox["incomingemaildeliverymethod"]).Value == 0)
            {
                TreeNode node = GetCurrentMailboxSettingNode();
                node.ImageIndex = 1;
                node.SelectedImageIndex = 1;
                node.ToolTipText = "The mailbox is currently not configured to automatically promote email. Open the mailbox and select an Incoming Email Delivery method.\n\nMailbox.IncomingEmailDeliveryMethod = 0 (None)";
                MailboxWarning = true;
            }
        }
        private TreeNode GetCurrentMailboxSettingNode()
        {
            int length = diagnosticTree.Nodes[1].Nodes[0].Nodes.Count - 1;
            TreeNode node = diagnosticTree.Nodes[1].Nodes[0].Nodes[length];
            return node;
        }
        private void DetermineEmailAddress()
        {
            if (EwsProxyFactory.ServiceEmailAddress.ToString() != (string)CrmOrgMailboxCache.TargetMailbox["emailaddress"])
            {
                TreeNode node = GetCurrentMailboxSettingNode();
                node.ImageIndex = 2;
                node.SelectedImageIndex = 2;
                node.ToolTipText = "The e-mail address of the mailbox record in CRM does not match the Exchange e-mail address that was provided.";
                MailboxError = true;
            }
        }
        private void DetermineEmailApproval()
        {
            TreeNode node = GetCurrentMailboxSettingNode();

            if (!MailboxApproved)
            {
                node.ImageIndex = 2;
                node.SelectedImageIndex = 2;
                node.ToolTipText = "The mailbox must be approved by a System Administrator before e-mail processing can occur.";
                MailboxError = true;
            }
        }
        private void BuildMisc()
        {
            TreeNode header = new TreeNode("Miscellaneous");
            diagnosticTree.Nodes.Add(header);

            if (MailboxUsingOutlook)
            {
                diagnosticTree.Nodes[2].Nodes.Add("Incoming Email Delivery Method is set to Outlook");
                DetermineOutlookPromotion();
                diagnosticTree.Nodes[2].Nodes.Add("Email Tagging is Disabled in CRM 2015+");
                DetermineEmailTagging();
                diagnosticTree.Nodes[2].Nodes.Add("Duplicate E-mail Addresses Found in CRM");
                DetermineDuplicateEmailAddresses();
            }
            else
            {
                diagnosticTree.Nodes[2].Nodes.Add("Duplicate E-mail Addresses Found in CRM");
                DetermineDuplicateEmailAddresses();
            }
        }
        private void DetermineOutlookPromotion()
        {
            diagnosticTree.Nodes[2].Nodes[0].ImageIndex = 1;
            diagnosticTree.Nodes[2].Nodes[0].SelectedImageIndex = 1;
            diagnosticTree.Nodes[2].Nodes[0].ToolTipText = "Emails will only be tracked into CRM when Outlook is running and the user has enabled Automatic Tracking (BackgroundReceive) via their Personal Options.";
            MiscWarning = true;
        }
        private void DetermineEmailTagging()
        {
            diagnosticTree.Nodes[2].Nodes[1].ImageIndex = 1;
            diagnosticTree.Nodes[2].Nodes[1].SelectedImageIndex = 1;
            diagnosticTree.Nodes[2].Nodes[1].ToolTipText = "E-mail Tagging is disabled by default in CRM 2015 and later versions. This can cause e-mails tracked by other users or queues to appear as untracked in Outlook.";
            MiscWarning = true;
        }
        private void DetermineDuplicateEmailAddresses()
        {
            bool multipleemails = RetrieveMailboxesbyEmail();

            if (MailboxUsingSSS)
            {
                if (multipleemails)
                {
                    diagnosticTree.Nodes[2].Nodes[0].ImageIndex = 2;
                    diagnosticTree.Nodes[2].Nodes[0].SelectedImageIndex = 2;
                    diagnosticTree.Nodes[2].Nodes[0].ToolTipText = "Multiple mailboxes with the same e-mail addresses were found. This can cause duplicate e-mail activity party members in CRM during email address resolution.";
                    MiscError = true;
                }
                else
                {
                    diagnosticTree.Nodes[2].Nodes[0].ImageIndex = 0;
                    diagnosticTree.Nodes[2].Nodes[0].SelectedImageIndex = 0;
                    diagnosticTree.Nodes[2].Nodes[0].ToolTipText = "No duplicate mailboxes with the same e-mail address found in CRM.";
                }
            }
            else
            {
                if (multipleemails)
                {
                    diagnosticTree.Nodes[2].Nodes[0].ImageIndex = 2;
                    diagnosticTree.Nodes[2].Nodes[0].SelectedImageIndex = 2;
                    diagnosticTree.Nodes[2].Nodes[0].ToolTipText = "Multiple mailboxes with the same e-mail addresses were found. This can cause duplicate e-mail activity party members in CRM during email address resolution.";
                    MiscError = true;
                }
                else
                {
                    diagnosticTree.Nodes[2].Nodes[0].ImageIndex = 0;
                    diagnosticTree.Nodes[2].Nodes[0].SelectedImageIndex = 0;
                    diagnosticTree.Nodes[2].Nodes[0].ToolTipText = "No duplicate mailboxes with the same e-mail address found in CRM.";
                }
            }
        }

        private bool RetrieveMailboxesbyEmail()
        {
            try
            {

                bool duplicatefound;

                QueryByAttribute query = new QueryByAttribute("mailbox");
                query.ColumnSet = new ColumnSet();
                query.Attributes.AddRange("emailaddress");
                query.Values.AddRange((string)CrmOrgMailboxCache.TargetMailbox["emailaddress"]);
                query.Attributes.AddRange("statecode");
                query.Values.AddRange(0);

                OrganizationServiceProxy orgProxy = CrmAuthInfoFactory.GetOrgProxy();
                EntityCollection mailboxes = orgProxy.RetrieveMultiple(query);

                if (mailboxes.Entities.Count == 1)
                {
                    duplicatefound = false;
                    return duplicatefound;
                }
                else
                {
                    duplicatefound = true;
                    return duplicatefound;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.StackTrace, e.Message);
                bool error = true;
                return error;
            }
        }
        private void SetHeaders()
        {
            if (OrgError)
            {
                diagnosticTree.Nodes[0].ImageIndex = 2;
                diagnosticTree.Nodes[0].SelectedImageIndex = 2;
            }
            else if (OrgWarning)
            {
                diagnosticTree.Nodes[0].ImageIndex = 1;
                diagnosticTree.Nodes[0].SelectedImageIndex = 1;
            }

            if (MailboxError)
            {
                diagnosticTree.Nodes[1].ImageIndex = 2;
                diagnosticTree.Nodes[1].SelectedImageIndex = 2;
                diagnosticTree.Nodes[1].Nodes[0].ImageIndex = 2;
                diagnosticTree.Nodes[1].Nodes[0].SelectedImageIndex = 2;
            }

            else if (MailboxWarning)
            {
                diagnosticTree.Nodes[1].ImageIndex = 1;
                diagnosticTree.Nodes[1].SelectedImageIndex = 1;
                diagnosticTree.Nodes[1].Nodes[0].ImageIndex = 1;
                diagnosticTree.Nodes[1].Nodes[0].SelectedImageIndex = 1;
            }

            if (MiscError)
            {
                diagnosticTree.Nodes[2].ImageIndex = 2;
                diagnosticTree.Nodes[2].SelectedImageIndex = 2;
            }
            else if (MiscWarning)
            {
                diagnosticTree.Nodes[2].ImageIndex = 1;
                diagnosticTree.Nodes[2].SelectedImageIndex = 1;
            }

            if (ACTError)
            {
                diagnosticTree.Nodes[1].ImageIndex = 2;
                diagnosticTree.Nodes[1].SelectedImageIndex = 2;
                diagnosticTree.Nodes[1].Nodes[0].ImageIndex = 2;
                diagnosticTree.Nodes[1].Nodes[0].SelectedImageIndex = 2;
                diagnosticTree.Nodes[1].Nodes[1].ImageIndex = 2;
                diagnosticTree.Nodes[1].Nodes[1].SelectedImageIndex = 2;
            }

            else if (ACTWarning)
            {
                diagnosticTree.Nodes[1].Nodes[1].ImageIndex = 1;
                diagnosticTree.Nodes[1].Nodes[1].SelectedImageIndex = 1;
                diagnosticTree.Nodes[1].Nodes[0].ImageIndex = 1;
                diagnosticTree.Nodes[1].Nodes[0].SelectedImageIndex = 1;
            }

        }

        private void MailboxDiagnostic_MouseClick(object sender, MouseEventArgs e)
        {
        }

        private void diagnosticTree_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node == diagnosticTree.Nodes[0] || e.Node == diagnosticTree.Nodes[1] || e.Node == diagnosticTree.Nodes[2])
            {
                txtBoxDescription.Text = "Select a child node to view more information";

            }
            else
            {
                txtBoxDescription.Text = e.Node.ToolTipText;
            }
        }
    }
}
